from django.shortcuts import render, HttpResponse, redirect
from utils.tencent.cos import delete_bucket
from web import models


def setting(request, project_id):
    return render(request, 'personal_management/setting.html')


def delete_item(request, project_id):
    bucket = request.tracer.project.bucket
    region = request.tracer.project.region
    # return HttpResponse('删除项目')
    if request.method == 'GET':
        return render(request, 'personal_management/setting_delete_item.html')
    project_name = request.POST.get('project_name')
    if not project_name or project_name != request.tracer.project.p_name:
        return render(request, 'personal_management/setting_delete_item.html', {'error': "项目名错误"})
    if request.tracer.user != request.tracer.project.creator:
        return render(request, 'personal_management/setting_delete_item.html', {'error': "您不是项目的创建者，无法删除项目"})
    delete_bucket(bucket, region)
    models.Project.objects.filter(id=request.tracer.project.id).delete()
    return redirect('websass:project_list')
